//
//  AuthWorkContentView.swift
//  认证 - 工作信息内容 View
//
//  Created by 千策万划-mini on 2025/5/7.
//

import Foundation

class AuthWorkContentView: UIView {
    
    var modelDetail: ModelAuthPersonalInfo = ModelAuthPersonalInfo()
    var blockUpdataModel:((ModelAuthPersonalInfo) -> Void)?
    
    // MARK: - lazy
    lazy var imageScheduleBg: UIImageView = {
        let result = UIImageView()
        result.image = UIImage(named: "icon_auth_schedule_bg")
        result.widthHeight = XY(SCREEN_WIDTH-W(15)*2, W(68))
        return result
    }()
    lazy var imageSchedule: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(W(329), W(38))
        return result
    }()
    lazy var viewContentBack: UIView = {
        let result = UIView()
        result.backgroundColor = UIColor(hex: "#BEAAFF")
        GlobalMethod.setRoundView(view: result, color: UIColor(hex: "#7B51FF"), numRound: 16, width: 4)
        result.width = SCREEN_WIDTH-W(15)*2
        return result
    }()
    
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.widthHeight = XY(SCREEN_WIDTH, 0)
        self.backgroundColor = .clear
        
        self.addSubView()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 添加
    func addSubView() {
        addSubview(imageScheduleBg)
        imageScheduleBg.addSubview(imageSchedule)
        addSubview(viewContentBack)
        
        resetView()
    }
    
    // MARK: - 重置界面
    func resetView(model: ModelAuthPersonalInfo = ModelAuthPersonalInfo()) {
        modelDetail = model
        imageScheduleBg.centerXTop = XY(SCREEN_WIDTH/2, W(16))
        let count = GlobalMethod.readInt(forKey: LOCAL_AUTH_PRODUCT_COUNT)
        imageSchedule.image = UIImage(named: count == 5 ? "icon_auth_schedule_3_5" : "icon_auth_schedule_3_4")
        imageSchedule.centerXCenterY = XY(imageScheduleBg.width/2, imageScheduleBg.height/2)
        
        var topMark = 6.0
        if(isAry(model.putting)){
            for i in 0..<model.putting.count {
                let modelItem = model.putting[i];
                if modelItem.imp == "Brown" {
                    // 输入框
                    let textFieldView = AuthTFView()
                    textFieldView.resetView(model: modelItem)
                    textFieldView.blockValueChange = { [weak self](model) in
                        guard let self = self else { return }
                        self.modelDetail.putting[i] = model
                        if let block = blockUpdataModel {
                            block(self.modelDetail)
                        }
                    }
                    textFieldView.centerXTop = XY(viewContentBack.width/2, topMark);
                    viewContentBack.addSubview(textFieldView)
                    topMark = textFieldView.bottom
                }
                if modelItem.imp == "hurried" {
                    // 单选
                    let singleChoiceView = SingleChoiceView()
                    singleChoiceView.resetView(model: modelItem)
                    singleChoiceView.pickerView.blockConfirmClick = { [weak self](selectValue, selectCode) in
                        guard let self = self else { return }
                        self.modelDetail.putting[i].pounding = selectValue
                        self.modelDetail.putting[i].wisest = selectCode
                        singleChoiceView.showContent(content: selectValue)
                        if let block = blockUpdataModel {
                            block(self.modelDetail)
                        }
                    }
                    singleChoiceView.centerXTop = XY(viewContentBack.width/2, topMark);
                    viewContentBack.addSubview(singleChoiceView)
                    topMark = singleChoiceView.bottom
                }
                if modelItem.imp == "farmhouse" {
                    // 地址选择框
                    let selectAddressView = SelectAddressView()
                    selectAddressView.resetView(model: modelItem)
                    selectAddressView.pickerView.blockConfirmClick = { [weak self](selectValue) in
                        guard let self = self else { return }
                        self.modelDetail.putting[i].pounding = selectValue
                        self.modelDetail.putting[i].wisest = selectValue
                        selectAddressView.showContent(content: selectValue)
                        if let block = blockUpdataModel {
                            block(self.modelDetail)
                        }
                    }
                    selectAddressView.centerXTop = XY(viewContentBack.width/2, topMark);
                    viewContentBack.addSubview(selectAddressView)
                    topMark = selectAddressView.bottom
                }
            }
            viewContentBack.height = topMark + W(20)
            viewContentBack.centerXTop = XY(SCREEN_WIDTH/2, imageScheduleBg.bottom+W(16))
        }
        
        height = viewContentBack.bottom+getBottomSafeAreaHeight()+60
    }
}
